var customer;
$(function () {
    //从localStorage中获取customer
    var custStr = localStorage.getItem("customer");
    customer = eval("("+ custStr +")");
    var data = "";
    var url;
    if(customer == null){
        alert("用户登陆超时，请重新登录");
        sessionStorage.removeItem("cart");
        window.location.href = "login/login.html";
    }
    var accountNo = customer.accountNo;
    // alert(accountNo);
    url = "orderInfo!showOrder";
    data += "accountNo=" + accountNo;
    sendRequest("post",url,data,showOrderBack);
    sendRequest("post","purchase!getPurchaseOrder",data,showOrderBack2);
});

function showOrderBack(result) {
    if(result == "\"no_item\""){

        alert("您还没有任何订单信息");
        window.location.href = "index.html";
    }

    sessionStorage.setItem("order",eval("(" + result + ")"));
    var rs = eval("("+ eval("("+ result +")") +")");
    console.log(rs);
    //全部
    var str = "";
    //已归还
    var str0 = "";
    //正在租用
    var str1 = "";
    //申请归还
    var str2 = "";
    //确认申请
    var str3 = "";

    //租用日期
    var rentTime;
    //计算已租天数
    var day;
    //计算租金
    var rent;
    for(var i=0;i<rs.length;++i){
        rentTime = new Date(rs[i].rentTime.replace(/-/g,"/"));
        day = new Date().getTime() - rentTime.getTime();
        day = parseInt(day/(1000*60*60*24));
        if(day <= 0)
            day = 1;
        rent = day * rs[i].rentPrice;
        if(rs[i].orderState == 0){
            var realPay = rs[i].rent;
            var returnMoney = rs[i].rentMoney - rs[i].rent > 0 ? rs[i].rentMoney - rs[i].rent : '0';
            str0 += "<div class=\"tab-item\">\n" +
                "                        <a href=\"javascript:void(0);\" class=\"aui-well-item aui-well-item-clear\">\n" +
                "                            <div class=\"aui-well-item-hd\">\n" +
                "                                <img src=\"order/images/icon-logo.png\" alt=\"\">\n" +
                "                            </div>\n" +
                "                            <div class=\"aui-well-item-bd\">\n" +
                "                                <h3>租乐宝</h3>\n" +
                "                            </div>\n" +
                "                            <span class=\"aui-well-item-fr\">已归还</span>\n" +
                "                        </a>\n" +
                "                        <div class=\"aui-mail-product\">\n" +
                "                            <a href=\"javascript:;\" class=\"aui-mail-product-item\">\n" +
                "                                <div class=\"aui-mail-product-item-hd\">\n" +
                "                                    <img src=\"data:image/jpg;base64,"+ rs[i].productPhoto +"\" alt=\"\">\n" +
                "                                </div>\n" +
                "                                <div class=\"aui-mail-product-item-bd\">\n" +
                "                                    <p>"+ rs[i].introduction +"</p>\n" +
                "                                </div>\n" +
                "                            </a>\n" +
                "                        </div>\n" +
                "                        <a href=\"javascript:;\" class=\"aui-mail-payment\">\n" +
                "                            <p>\n" +
                "                                共<em>1</em>\n" +
                "                                件商品 实付款: ￥<i>"+ realPay +"</i>&nbsp;&nbsp;返押金: ￥<i>"+ returnMoney +"</i>\n" +
                "                            </p>\n" +
                "                        </a>\n" +
                "                        <div class=\"aui-mail-button\">\n" +
                "                            <a href=\"javascript:;\" onclick=''>再次租用</a>\n" +
                "                        </div>\n" +
                "                    </div>";
        }
        if(rs[i].orderState == 1){
            str1 += "<div class=\"tab-item\">\n" +
                "                        <a href=\"javascript:void(0);\" class=\"aui-well-item aui-well-item-clear\">\n" +
                "                            <div class=\"aui-well-item-hd\">\n" +
                "                                <img src=\"order/images/icon-logo.png\" alt=\"\">\n" +
                "                            </div>\n" +
                "                            <div class=\"aui-well-item-bd\">\n" +
                "                                <h3>租乐宝</h3>\n" +
                "                            </div>\n" +
                "                            <span class=\"aui-well-wait\">正在租用</span>\n" +
                "                        </a>\n" +
                "                        <div class=\"aui-mail-product\">\n" +
                "                            <a href=\"javascript:;\" class=\"aui-mail-product-item\">\n" +
                "                                <div class=\"aui-mail-product-item-hd\">\n" +
                "                                    <img src=\"data:image/jpg;base64,"+ rs[i].productPhoto +"\" alt=\"\">\n" +
                "                                </div>\n" +
                "                                <div class=\"aui-mail-product-item-bd\">\n" +
                "                                    <p>"+ rs[i].introduction +"</p>\n" +
                "                                </div>\n" +
                "                            </a>\n" +
                "                        </div>\n" +
                "                        <a href=\"javascript:;\" class=\"aui-mail-payment\">\n" +
                "                            <p>\n" +
                "                                已租用<em>"+ day +"</em>\n" +
                "                                天 实付款: ￥<i>"+ rent +"</i>\n" +
                "                            </p>\n" +
                "                        </a>\n" +
                "                        <div day='"+ day +"' rent='"+ rent +"' time='"+ rs[i].rentTime +"' id='"+ rs[i].productQR +"' orders='"+ rs[i].rentOrderId +"' name='"+ rs[i].accountNo +"' onclick='fun(this)'  class=\"aui-mail-button\">\n" +
                "                            <a href=\"javascript:;\">归还商品</a>\n" +
                //      "                            <a href=\"javascript:;\" class=\"aui-df-color\">再次购买</a>\n" +
                //      "                            <a href=\"javascript:;\" class=\"aui-df-color\">查看物流</a>\n" +
                "                        </div>\n" +
                "                    </div>";
        }

        if(rs[i].orderState == 2){
            var realPay = rs[i].rent;
            var returnMoney = rs[i].rentMoney - rs[i].rent > 0 ? rs[i].rentMoney - rs[i].rent : '0';
            str2 += "<div class=\"tab-item\">\n" +
                "                        <a href=\"javascript:void(0);\" class=\"aui-well-item aui-well-item-clear\">\n" +
                "                            <div class=\"aui-well-item-hd\">\n" +
                "                                <img src=\"order/images/icon-logo.png\" alt=\"\">\n" +
                "                            </div>\n" +
                "                            <div class=\"aui-well-item-bd\">\n" +
                "                                <h3>租乐宝</h3>\n" +
                "                            </div>\n" +
                "                            <span class=\"aui-well-item-fr\">申请归还中</span>\n" +
                "                        </a>\n" +
                "                        <div class=\"aui-mail-product\">\n" +
                "                            <a href=\"javascript:;\" class=\"aui-mail-product-item\">\n" +
                "                                <div class=\"aui-mail-product-item-hd\">\n" +
                "                                    <img src=\"data:image/jpg;base64,"+ rs[i].productPhoto +"\" alt=\"\">\n" +
                "                                </div>\n" +
                "                                <div class=\"aui-mail-product-item-bd\">\n" +
                "                                    <p>"+ rs[i].introduction +"</p>\n" +
                "                                </div>\n" +
                "                            </a>\n" +
                "                        </div>\n" +
                "                        <a href=\"javascript:;\" class=\"aui-mail-payment\">\n" +
                "                            <p>\n" +
                "                                共<em>1</em>\n" +
                "                                件商品 实付款: ￥<i>"+ realPay +"</i>&nbsp;&nbsp;返押金: ￥<i>"+ returnMoney +"</i>\n" +
                "                            </p>\n" +
                "                        </a>\n" +
                "                        <div class=\"aui-mail-button\">\n" +
                "                            <a href=\"javascript:;\" onclick='cancelSupply("+ rs[i].productQR +","+ rs[i].accountNo +","+ rs[i].rentOrderId +")'>取消申请</a>\n" +
                "                        </div>\n" +
                "                    </div>";
        }

        if(rs[i].orderState == 3){
            var realPay = rs[i].rent;
            var returnMoney = rs[i].rentMoney - rs[i].rent > 0 ? rs[i].rentMoney - rs[i].rent : '0';
            str3 += "<div class=\"tab-item\">\n" +
                "                        <a href=\"javascript:void(0);\" class=\"aui-well-item aui-well-item-clear\">\n" +
                "                            <div class=\"aui-well-item-hd\">\n" +
                "                                <img src=\"order/images/icon-logo.png\" alt=\"\">\n" +
                "                            </div>\n" +
                "                            <div class=\"aui-well-item-bd\">\n" +
                "                                <h3>租乐宝</h3>\n" +
                "                            </div>\n" +
                "                            <span class=\"aui-well-item-fr\">归还已确认</span>\n" +
                "                        </a>\n" +
                "                        <div class=\"aui-mail-product\">\n" +
                "                            <a href=\"javascript:;\" class=\"aui-mail-product-item\">\n" +
                "                                <div class=\"aui-mail-product-item-hd\">\n" +
                "                                    <img src=\"data:image/jpg;base64,"+ rs[i].productPhoto +"\" alt=\"\">\n" +
                "                                </div>\n" +
                "                                <div class=\"aui-mail-product-item-bd\">\n" +
                "                                    <p>"+ rs[i].introduction +"</p>\n" +
                "                                </div>\n" +
                "                            </a>\n" +
                "                        </div>\n" +
                "                        <a href=\"javascript:;\" class=\"aui-mail-payment\">\n" +
                "                            <p>\n" +
                "                                共<em>1</em>\n" +
                "                                件商品 实付款: ￥<i>"+ realPay +"</i>&nbsp;&nbsp;返押金: ￥<i>"+ returnMoney +"</i>\n" +
                "                            </p>\n" +
                "                        </a>\n" +
   //             "                        <div class=\"aui-mail-button\">\n" +
    //            "                            <a href=\"javascript:;\" onclick='cancelSupply("+ rs[i].productQR +","+ rs[i].accountNo +","+ rs[i].rentOrderId +")'>取消申请</a>\n" +
    //            "                        </div>\n" +
                "                    </div>";
        }
    }
    $("#renting").html(str1);
    $("#supply").html(str2);
    $("#returned").html(str0);
    $("#confirmSupply").html(str3);
    str = str1 + str0 + str2 + str3;
    $("#all").html(str);
}
function showOrderBack2(result) {
    var buyString = '';
    if (result == "purchase_null"){
        alert('您还没有购买订单！');
    }else{
        sessionStorage.setItem("order",eval("(" + result + ")"));
        var rs = eval("("+ eval("("+ result +")") +")");
        for(var i=0;i<rs.length;++i) {
            var tag = '已付款';
            if(rs[i].orderState == '1') tag = '已付款';
            if(rs[i].orderState == '2') tag = '已发货';
            if(rs[i].orderState == '3') tag = '已收货';
            buyString += "<div class=\"tab-item\">\n" +
                "                        <a href=\"javascript:void(0);\" class=\"aui-well-item aui-well-item-clear\" onclick=\"deleteOrder(this)\">\n" +
                "                            <div class=\"aui-well-item-hd\">\n" +
                "                                <img src=\"order/images/icon-logo.png\" alt=\"\">\n" +
                "                            </div>\n" +
                "                            <div class=\"aui-well-item-bd\">\n" +
                "                                <h3>租乐宝</h3>\n" +
                "                            </div><input type='hidden' name='orderId' value='"+rs[i].orderId+"'><input type='hidden' value='"+rs[i].productQR+"'><input type='hidden' value='"+rs[i].accountNo+"'>\n" +
                "                            <span class=\"aui-well-item-fr\" style='color: #f0250f'>"+tag+"</span>\n" +
                "                        </a>\n" +
                "                        <div class=\"aui-mail-product\">\n" +
                "                            <a href=\"javascript:;\" class=\"aui-mail-product-item\">\n" +
                "                                <div class=\"aui-mail-product-item-hd\">\n" +
                "                                    <img src=\"data:image/jpg;base64," + rs[i].productPhoto + "\" alt=\"\">\n" +
                "                                </div>\n" +
                "                                <div class=\"aui-mail-product-item-bd\">\n" +
                "                                    <p>" + rs[i].productName + "</p>\n" +
                "                                </div>\n" +
                "                            </a>\n" +
                "                        </div>\n" +
                "                        <a href=\"javascript:;\" class=\"aui-mail-payment\">\n" +
                "                            <p>\n" +
                "                                共<em>"+rs[i].buyNumber+"</em>\n" +
                "                                件商品 实付款: ￥<i>" + rs[i].price +
                "                            </i></p>\n" +
                "                        </a>\n" +
             /*   "                        <div class=\"aui-mail-button\">\n" +
                "                            <a href=\"javascript:;\">再次购买</a>\n" +*/
                "                        </div>\n" +
                "                    </div>";
        }
    }
    //console.log(buyString);
    //console.log(result);
    $("#buy").html(buyString);
}
function fun(dt) {
    if(confirm("您确定要归还吗？")){
        console.log($(dt).attr("id"));
        console.log($(dt).attr("orders"));
        console.log($(dt).attr("name"));
        console.log($(dt).attr("time"));
        console.log($(dt).attr("rent"));
        console.log($(dt).attr("day"));

        var data = "";
        data += "accountNo=" + $(dt).attr("name") +
            "&productQR=" + $(dt).attr("id") +
            "&rentOrderId=" + $(dt).attr("orders") +
            "&rentTime=" + $(dt).attr("time") +
            "&rent=" + $(dt).attr("rent") +
            "&realRentDays=" + $(dt).attr("day");
        var url = "/orderInfo!supplyReturnProduct";
        sendRequest("post",url,data,supplyReturnProductBack);
    }
}

function cancelSupply(productQR,accountNo,rentOrderId) {
    console.log(productQR);
    console.log(accountNo);
    console.log(rentOrderId);

    var data = "";
    data += "productQR=" + productQR +
            "&accountNo=" + accountNo +
            "&rentOrderId=" + rentOrderId;
    var url = "/orderInfo!cancelSupply"

}

function supplyReturnProductBack(result) {
    if(result == "\"update_success\""){
        alert("归还申请已提交，工作人员将会在三个工作日内和您联系，祝您生活愉快！");
        window.location.reload(true);
    }
    else{
        alert("归还申请提交失败！");
    }
}

function remove() {
    //封装订单信息
    //用户信息
    if(confirm("确认删除吗？")){
        var pros = [];
        var custStr = localStorage.getItem("customer");
        var customer = eval("("+ custStr +")");
        if(customer == null){
            alert("用户登陆超时，请重新登录");
            window.location.href = "login/login.html";
        }
        var cart = sessionStorage.getItem("cart");
        var proInfo = eval("("+ cart +")").products;
        //console.log(proInfo);
        for(var i=0;i<proInfo.length;++i){
            //console.log($("#" + proInfo[i].productQR).hasClass("pitch_on"));
            if($("#" + proInfo[i].productQR).hasClass("pitch_on")){

                pros.push(proInfo[i]);
                //alert(proInfo[i].productQR);
            }
        }

        console.log(pros);

        if(pros.length <= 0){
            alert("请选择要删除的商品");
        }else{
            var proStr = JSON.stringify(pros);
            $.ajax({
                url:'/rentCart!deleteItem',
                type:'post',
                data:{'accountNo':customer.accountNo,
                    'proStr':proStr
                },
                //dataType:"json",
                success:function(data){
                    //alert(data);
                    //随便的显示一下传回的数据喽
                    if(data == "success"){
                        alert("删除成功");
                        window.location.reload();
                    }
                }
            });
        }
    }



}

function deleteOrder(obj){
    if(confirm("您确定删除订单吗？")) {
        //console.log($(obj).find('span').html());
        var x = $(obj).find('input');
        /* console.log(x[0].value);
         console.log(x[1].value);
         console.log(x[2].value);*/

        //alert(productQR);

        var url = "customer!deleteOrder";

        var data = "productQR=" + x[1].value + "&accountNo=" + x[2].value + "&orderId=" + x[0].value;
        console.log(data);
        sendRequest("post", url, data, deleteBack);
    }
}

function deleteBack(result){
    //console.log(result);
    if(result == "delete_success"){
        alert('订单已删除！');
        showOrderBack2();
    }
}